package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.base.util.PasswordUtil;
import cn.tedu.ivos.user.service.UserService;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQueryParam;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.enity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("userLoginParam:{}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("userVO:{}", userVO);
        return userVO;
    }

    @Override
    public List<UserVO> selectUser(UserQueryParam userQueryParam) {
        return userMapper.selectUserByUsernameAndStatus(userQueryParam);
    }
    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("userSaveParam:{}", userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);
        if(user.getId()!=null){
            log.debug("修改用户业务");
            user.setUpdateTime(new Date());
            userMapper.updateUser(user);
        }else {
            log.debug("保存用户业务");
            user.setPassword("123456");
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            userMapper.insertUser(user);
        }
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码业务，userId:{}", userId);
        User user = new User();
        user.setId(userId);
//        user.setPassword("123456");
        user.setPassword(PasswordUtil.generateRandomPassword(6));
        userMapper.updateUser(user);
    }

    @Override
    public void changeStatus(Long userId, String status) {
        log.debug("修改用户状态业务，userId:{},status:{}", userId, status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Long id) {
        log.debug("删除用户业务，id:{}", id);
        userMapper.deleteUser(id);
    }

    @Override
    public List<UserVO> selectAuditUser(Long parentId) {
        log.debug("查询审核用户业务，parentId:{}", parentId);
        ArrayList<UserVO> list = new ArrayList<>();
        UserVO auditUserVO1 = userMapper.selectById(parentId);
        list.add(auditUserVO1);
        if (auditUserVO1 != null && auditUserVO1.getParentId() != null){
            UserVO auditUserVO2 = userMapper.selectById(auditUserVO1.getParentId());
            list.add(auditUserVO2);
        }
        return list;
    }
}
